import {
  apiUrl
} from '../../config/index'

Page({
  data: {
    inputValue: '',
    output: ['Tip:每次会话保持30分钟，.clear重置会话'],
    sysToken: wx.getStorageSync('token')
  },
  submitCommand() {
    let command = this.data.inputValue
    console.log('cmd:' + command)
    if (!command) {
      return
    }
    let output = this.data.output
    output.unshift('> ' + command);
    this.setData({
      output,
      inputValue: ''
    })
    wx.request({
      url: apiUrl + '/magic/exec',
      method: 'POST',
      data: {
        "prompt": command
      },
      header: {
        sysToken: this.data.sysToken
      },
      success: (result) => {
        let res = result.data
        if (res.code == 1) {
          if (command == '.clear') {
            this.setData({
              output: ['会话已重置'],
              inputValue: ''
            })
            return
          } else {
            let output = this.data.output
            output.unshift(res.data);
            this.setData({
              output
            })
          }
        } else {
          let output = this.data.output
          output.unshift('Err:' + (res.data || res.message))
          this.setData({
            output
          })
        }
      }
    })
  },
  handleInputChange(e) {
    this.setData({
      inputValue: e.detail.value
    });
  }
})